from sqlalchemy.orm import sessionmaker
from models.models import Student
from config.config import Config


class StudentController:
    def __init__(self):
        self.engine = Config.engine
        self.Session = sessionmaker(bind=self.engine)

    def add_student(self, sno, sname, ssex, sage, dno, sclass, address):
        session = self.Session()
        student = Student(Sno=sno, Sname=sname, Ssex=ssex, Sage=sage, Dno=dno, Sclass=sclass, address=address)
        session.add(student)
        session.commit()
        session.close()

    def delete_student_by_sno(self, sno):
        session = self.Session()
        student = session.query(Student).filter_by(Sno=sno).first()
        if student:
            session.delete(student)
            session.commit()
        session.close()

    def query_student_by_name(self, name):
        session = self.Session()
        students = session.query(Student).filter(Student.Sname.like('%' + name + '%')).all()
        session.close()
        return students